
//
//  HMBaseToolbar.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/14.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMBaseToolbar.h"
#import "HMStatus.h"

@interface HMBaseToolbar()

//按钮数组
@property (nonatomic, strong) NSMutableArray *btnArray;
//转发
@property (nonatomic, weak) UIButton *retweetBtn;
//评论
@property (nonatomic, weak) UIButton *commentBtn;
//点赞
@property (nonatomic, weak) UIButton *likeBtn;
//分割线 数组
@property (nonatomic, strong) NSMutableArray *dividers;

@end

@implementation HMBaseToolbar
//懒加载
- (NSMutableArray *)btnArray{
    if (_btnArray == nil) {
        self.btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (NSMutableArray *)dividers{
    if (_dividers == nil) {
        self.dividers = [NSMutableArray array];
    }
    return _dividers;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.设置 toolbar
        [self setupToolbar];
        
        //2.设置 按钮
        [self setupButtons];
    }
    return self;
}

#pragma mark - 1.设置 toolbar
- (void)setupToolbar{
    self.image = [UIImage resizedImage:@"timeline_card_bottom_background"];
    self.userInteractionEnabled = YES;
}

#pragma mark - 2.设置 按钮
- (void)setupButtons{

    self.retweetBtn = [self setupBtnWithIcon:@"timeline_icon_retweet" title:@"转发"];
    self.commentBtn = [self setupBtnWithIcon:@"timeline_icon_comment" title:@"评论"];
    self.retweetBtn = [self setupBtnWithIcon:@"timeline_icon_unlike" title:@"赞"];
    
    //设置第一条分割线
    [self setupDivider];
    
    //设置第二条分割线
    [self setupDivider];
}

- (void)setupDivider{
    UIImageView *divider = [[UIImageView alloc] init];
    divider.image = [UIImage imageWithName:@"timeline_card_bottom_line"];
    divider.contentMode = UIViewContentModeCenter;
    [self addSubview:divider];
    
    [self.dividers addObject:divider];
}

- (UIButton *)setupBtnWithIcon:(NSString *)icon title:(NSString *)title{

    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    //设置高亮时的背景
    [btn setBackgroundImage:[UIImage resizedImage:@"common_card_bottom_background_highlighted"] forState:UIControlStateHighlighted];
    btn.adjustsImageWhenHighlighted = NO;
    //设置间距
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [self addSubview:btn];
    
    [self.btnArray addObject:btn];
    
    return btn;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    
    //设置按钮的frame
    int btnCount = (int)self.btnArray.count;
    CGFloat btnW = self.width / btnCount;
    CGFloat btnH = self.height;
    
    for (int i = 0; i < btnCount; i++) {
        UIButton *btn = self.btnArray[i];
        btn.width = btnW;
        btn.height = btnH;
        btn.y = 0;
        btn.x= i * btnW;
    }
    
    //设置分割线的frame
    int divCount = (int)self.dividers.count;
    
    for (int i = 0; i < divCount; i++) {
        UIImageView *divider = self.dividers[i];
        divider.width = 1;
        divider.height = btnH;
        divider.centerX = (i + 1) * btnW;
        divider.centerY = btnH * 0.5;
    }
}

#pragma mark - 重写 status 的setter 方法
- (void)setStatus:(HMStatus *)status{
    _status = status;
    
    //转发 reposts_count如果存在,直接显示reposts_count;如果不存在,显示"转发"
    [self setupBtnTitle:self.retweetBtn count:status.reposts_count defaultTitle:@"转发"];
    //评论
    [self setupBtnTitle:self.commentBtn count:status.comments_count defaultTitle:@"评论"];
    //点赞
    [self setupBtnTitle:self.likeBtn count:status.attitudes_count defaultTitle:@"点赞"];
    
}

- (void)setupBtnTitle:(UIButton *)button count:(int)count defaultTitle:(NSString *)defaultTitle{
   //判断评论字数
    //1233212 -> 123.3万
    //1233712 -> 123.4万
    //1230000 ->123.0万 但是为整数的时候,不需要后面的'.0',用stringByReplacingOccurrencesOfString
    
    if (count >= 10000 ) {
        //转化为汉字万(保留一位小数)
        defaultTitle = [NSString stringWithFormat:@"%.1f万", count/ 10000.0];
        //用空串替换掉所有的.0(如果是2.0万的情况,则直接写为2万)
        defaultTitle = [defaultTitle stringByReplacingOccurrencesOfString:@".0" withString:@""];
    }else if(count > 0){
        //(0, 10000)
        defaultTitle = [NSString stringWithFormat:@"%d", count];
    }
    [button setTitle:defaultTitle forState:UIControlStateNormal];

}



@end
